package com.glsc.ngateway.opmanage.controller;

import com.glsc.ngateway.common.api.common.enums.DictConstant;
import com.glsc.ngateway.common.base.domain.mysql.gateway.other.EmailClient;
import com.glsc.ngateway.common.api.common.ex.Result;
import com.glsc.ngateway.opmanage.utils.RequestTool;
import com.glsc.ngateway.opmanage.service.EmailClientService;
import com.glsc.ngateway.opmanage.service.MailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: zhaoxc
 * @Date: 2020/7/20 13:21
 * @Desc: 邮件controller
 */
@RestController
@RequestMapping("api/mail")
@Api(value = "邮件controller", tags = {"邮件接口"})
public class EmailClientController {

    @Autowired
    private EmailClientService emailClientService;

    @Autowired
    private MailService mailService;

    @Resource
    private RequestTool requestTool;

    @GetMapping("/listpage")
    @ApiOperation(value = "查询邮件分页列表", notes = "查询邮件分页列表")
    public Result<Page<EmailClient>> findListPage(@ApiParam(required = false, value = "开始时间") @RequestParam(required = false) String beginTime,
                                                  @ApiParam(required = false, value = "结束时间") @RequestParam(required = false) String endTime,
                                                  @ApiParam(required = false, value = "邮件状态 字典：邮件状态 am_mail_status") @RequestParam(required = false) String status,
                                                  @ApiParam(required = false, value = "邮件接收邮箱") @RequestParam(required = false) String recipient,
                                                  @ApiParam(required = false, value = "邮件名称") @RequestParam(required = false) String title,
                                                  @ApiParam(required = false, value = "邮件内容") @RequestParam(required = false) String mailContent,
                                                  @ApiParam(required = false, value = "页码 1开始") @RequestParam(required = false) Integer pageNo,
                                                  @ApiParam(required = false, value = "每页显示条数 默认10") @RequestParam(required = false) Integer pageSize) {

        Map<String, Object> param = new HashMap<>();
        if (null == pageNo || pageNo < 1) {
            pageNo = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        if (null != beginTime && beginTime.length() > 0) {
            param.put("GT_opTime", beginTime);
        }
        if (null != endTime && endTime.length() > 0) {
            param.put("LT_opTime", endTime);
        }
        if (null != status && status.length() > 0) {
            param.put("EQ_status", status);
        }
        if (null != recipient && recipient.length() > 0) {
            param.put("LIKE_recipient", recipient);
        }
        if (null != title && title.length() > 0) {
            param.put("LIKE_title", title);
        }
        if (null != mailContent && mailContent.length() > 0) {
            param.put("LIKE_content", mailContent);
        }
        Page<EmailClient> list = emailClientService.findPage(pageNo, pageSize, param);
        if (null != list && list.getTotalElements() > 0) {
            return Result.successData(list);
        } else {
            return Result.failData("没有查询到数据");
        }
    }

    @GetMapping("/resend")
    @ApiOperation(value = "重发邮件", notes = "重发邮件")
    public Result resendEmail(@ApiParam(required = true, value = "邮件id") @RequestParam(required = false) Integer mailId) {
        EmailClient emailClient = emailClientService.findById(mailId);
        if(Objects.isNull(emailClient)){
            return Result.failData(String.format("未找到编码为%s邮件", emailClient.getMailId()));
        }
        emailClient.setOpTime(LocalDateTime.now());
        emailClient.setOpOperator(requestTool.getAccountFromRequest());
        try {
            String result = mailService.reSendMail(emailClient);
            if (DictConstant.YES.equals(result)) {
                emailClient.setStatus(DictConstant.MAIL_STATUS_SUCCESS);
                return Result.successData("编码为" + emailClient.getMailId() + "的邮件重发成功");
            } else {
                emailClient.setStatus(DictConstant.MAIL_STATUS_FAIL);
                return Result.failData("编码为" + emailClient.getMailId() + "的邮件重发失败");
            }
        } catch (Exception e) {
            emailClient.setStatus(DictConstant.MAIL_STATUS_TODO);
            return Result.failData("编码为" + emailClient.getMailId() + "的邮件重发失败");
        }finally {
            emailClientService.saveOrUpdate(emailClient);
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改邮件", notes = "修改邮件发送记录")
    public Result update(@ApiParam(required = false, value = "修改邮件内容") @RequestBody(required = false) EmailClient updateEmailClient) {
        if (updateEmailClient == null || updateEmailClient.getMailId() == null
                || StringUtils.isEmpty(updateEmailClient.getContent())
        ) {
            return Result.failData("修改邮件记录时，邮件信息、邮件ID不能为空");
        }
        updateEmailClient.setOpTime(LocalDateTime.now());
        updateEmailClient.setOpOperator(requestTool.getAccountFromRequest());
        return Result.successData(emailClientService.saveOrUpdate(updateEmailClient));
    }

}
